//
//  ALPS_RecommendMoreTableViewCell.m
//  Alps
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_RecommendMoreTableViewCell.h"
#import "ALPS_MainListenCollectionViewCell.h"
#import "ALPS_MoreModel.h"

@interface ALPS_RecommendMoreTableViewCell ()
/** 存放model的数组 */
@property (nonatomic, strong) NSMutableArray *arr;
@end

@implementation ALPS_RecommendMoreTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.leftArrow = [[UIImageView alloc] init];
        [self.contentView addSubview:_leftArrow];
        
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_titleLabel];
        
        self.moreLabel = [[UILabel alloc] init];
        self.moreLabel.font = [UIFont systemFontOfSize:15];
        self.moreLabel.textColor = [UIColor grayColor];
        self.moreLabel.textAlignment = 2;
        [self.contentView addSubview:_moreLabel];
        
        self.lineLabel = [[UILabel alloc] init];
        self.lineLabel.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
        [self.contentView addSubview:_lineLabel];
    }
    return self;
}

- (void)setModel:(ALPS_MainModel *)model
{
    if (_model != model) {
        _model = model;
        self.titleLabel.text = model.title;
        self.moreLabel.text = @"更多";
        self.leftArrow.image = [UIImage imageNamed:@"toolbar_play_n"];
        self.arr = [NSMutableArray array];
        for (NSDictionary *dic in self.model.list) {
            ALPS_MoreModel *model = [[ALPS_MoreModel alloc] initWithDic:dic];
            [self.arr addObject:model];
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(ScreeWidth, ScreeHeight / 6.67);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    if (self.section == 0 && self.categoryId != TALK_SHOW) {
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, ScreeHeight / 16, ScreeWidth, ScreeHeight / 1.02 - ScreeHeight / 16 - 10) collectionViewLayout:flowLayout];
        
        [self.leftArrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(ScreeHeight / 58 - 10, 0, ScreeHeight / 1.02 - ScreeHeight / 13 + 10, ScreeWidth - ScreeHeight / 16.675));
        }];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(5, 30, ScreeHeight / 1.02 - ScreeHeight / 12.13 + 15, 150));
        }];

        [self.moreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(- 10, 200, ScreeHeight / 1.02 - ScreeHeight / 12.13 , 15));
        }];
        
        [self.lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(ScreeHeight / 1.02 - 10, 0, 0, 0));
        }];
    } else {
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, ScreeHeight / 16, ScreeWidth, ScreeHeight / 1.9 - ScreeHeight / 16 - 10) collectionViewLayout:flowLayout];
        
        [self.leftArrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(ScreeHeight / 58 - 10, 0, ScreeHeight / 1.9 - ScreeHeight / 13 + 10, ScreeWidth - ScreeHeight / 16.675));
        }];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(5, 30, ScreeHeight / 1.9 - ScreeHeight / 12.13 + 15, 150));
        }];
        
        [self.moreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(- 10, 200, ScreeHeight / 1.9 - ScreeHeight / 12.13 , 15));
        }];
        
        [self.lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(ScreeHeight / 1.9 - 10, 0, 0, 0));
        }];
    }
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.scrollEnabled = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:_collectionView];
    
    [self.collectionView registerClass:[ALPS_MainListenCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_MainListenCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.moreModel = [self.arr objectAtIndex:indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_MoreModel *model = [self.arr objectAtIndex:indexPath.row];
    if (model.tracks != 0) {
        _block(model.albumId);        
    } else {
        _specialBlock(model.specialId);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
